home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 07 - 1991 / 07.11 Nov 91 / TPose Source / PatchWorks < prev    next >
Encoding:
Text File  |  1990-09-20  |  5.9 KB  |  207 lines  |  [TEXT/MPS ]

  1. #
  2. #    PatchWorks -
  3. #        Applies changes described in a set of patch files.
  4. #
  5. #    Usage:
  6. #        PatchWorks [-create | -restore]
  7. #            - create        # creates a set of patch files
  8. #            - restore        # restores patched files from saved originals
  9. #
  10. #    Returns:
  11. #        Not much
  12. #
  13. #    To create the patch file, use:
  14. #
  15. #        Compare -v <newfile> <oldfile> > <oldfile>.Df
  16. #
  17. #        Or
  18. #
  19. #        PatchWorks -create
  20. #
  21. #    To apply the patches, use:
  22. #
  23. #        PatchWorks
  24. #
  25. #    and select the directories containing the files to be converted and the
  26. #    patch files.  The script will perform the conversion and rename the
  27. #    original file to "<file>.ol".
  28. #
  29. #    If, for some reason, you need to revert to your original set of source
  30. #    files, you can use "PatchWorks -restore". This will replace the patched
  31. #    files with the saved <file>.ol files.
  32. #
  33. #
  34. #    Change History
  35. #    6/30/90        lsr        First release.
  36. #    8/01/90        kaar    Added "-create" option to create patch files. Put file
  37. #                        extensions into variables. Made to work with nested folders
  38. #                        (both during creation of the patch files, and during
  39. #                        application of changes).
  40. #    8/23/90        kaar    Allow people to cancel from StdFile. Made about 25% faster
  41. #                        by not switching targets. Hopefully fixed infinite loop bug.
  42. #    8/29/90        kaar    Added restore option (replaces files with any .ol versions)
  43. #                        Allow for missing source files (i.e, files that need to be
  44. #                        patched). This can happen if people haven't installed the
  45. #                        C++ support.
  46. #    8/30/90        kaar    Have -create ask for folder names, rather than hard-coding
  47. #                        them. Remove the resource forks of the patch files to make
  48. #                        them smaller for electronic transmission.
  49. #    9/19/90        kaar    Name of the patches folder is no longer hard-coded. It is
  50. #                        now "<original folder name> Patches". Send diagnostic output
  51. #                        of "which" to bit bucket.
  52. #
  53.  
  54. Set Exit 0
  55.  
  56. set DiffSuffix        ".df"    # Gotta be only 3 chars so we can tack it onto "Build Rules and Dependencies" and still stay under 32 characters.
  57. set OriginalSuffix    ".ol"
  58. set ConvertedSuffix    ".cv"
  59.  
  60. If "{1}" == "-create"
  61.  
  62.     Set originalFolder `getfilename -d -m "Select original folder." || Echo '""'`
  63.     Exit If "{originalFolder}" == ""
  64.     Set updatedFolder `getfilename -d -m "Select udpated folder." || Echo '""'`
  65.     Exit If "{updatedFolder}" == ""
  66.     Set patchesFolder `getfilename -d -m "Select location for patches folder." || Echo '""'`
  67.     Exit If "{patchesFolder}" == ""
  68.  
  69.     (Evaluate "{originalFolder}" =~ /≈:(≈)®1:/) > Dev:Null
  70.     Set patchesFolder "{patchesFolder}{®1} Patches:"
  71.  
  72.     For fullFileName in `files -r -s -f "{originalFolder}" -t TEXT`
  73.         (Evaluate "{fullFileName}" =~ /{originalFolder}(≈)®1/) > Dev:Null
  74.         Set fileName "{®1}"
  75.  
  76.         (Evaluate "{patchesFolder}{fileName}" =~ /(≈:)®1≈/) > Dev:Null
  77.         If `Exists -d "{®1}"` == ""
  78.             Set oldDirectory `Directory` ∑ Dev:Null            # save the directory
  79.             Set partialPath "{patchesFolder}{fileName}"
  80.             Loop
  81.                 (Evaluate "{partialPath}" =~ /(:*[¬:]+)®1(≈)®2/) > Dev:Null
  82.                 Break If "{®2}" == ""
  83.                 Set partialPath "{®2}"
  84.                 If `Exists -d "{®1}:"` == ""
  85.                     Set dir "`Directory -q`"
  86.                     (Evaluate "{dir}" =~ /(≈)®3:/) > Dev:Null
  87.                     Echo "Creating ‘{®3}{®1}:’"
  88.                     NewFolder "{®1}:"
  89.                 End
  90.                 Directory "{®1}:"
  91.             End
  92.             Directory "{oldDirectory}"                        # restore the original directory
  93.         End
  94.  
  95.         Echo -n "Comparing {fileName}."
  96.         Compare -v "{updatedFolder}{fileName}" "{originalFolder}{fileName}" > "{patchesFolder}{fileName}{DiffSuffix}" ≥≥ Dev:Null
  97.         Set savedStatus "{Status}"
  98.         If "{savedStatus}" != 2
  99.             If "{savedStatus}" == 0
  100.                 Echo -n " Files are the same."
  101.             Else
  102.                 Echo -n " Compare error #{savedStatus}."
  103.             End
  104.             Echo " Deleting ..."
  105.             Delete -y "{patchesFolder}{fileName}{DiffSuffix}"
  106.         Else
  107.             Echo
  108.             If "`which NukeResForks ≥ Dev:Null`" != ""
  109.                 NukeResForks "{patchesFolder}{fileName}{DiffSuffix}"
  110.             End
  111.         End
  112.     End
  113.  
  114. Else If "{1}" == "-restore"
  115.     Set iDir `getfilename -d -m 'Select MacApp folder to restore.'`
  116.     Exit If "{iDir}" == ""
  117.     For file In `files -r -s -f "{iDir}" -t TEXT`
  118.         If "{file}" =~ /{iDir}(≈)®1{OriginalSuffix}/
  119.             Set src "{iDir}{®1}"
  120.             Set Original "{src}{OriginalSuffix}"
  121.  
  122.             Echo "Restoring {src}."
  123.  
  124.             Delete -y "{src}"
  125.             Rename "{Original}" "{src}"
  126.         End
  127.     End
  128.  
  129. Else
  130.     Set iDir `getfilename -d -m 'Select folder to be patched.'`
  131.     Exit If "{iDir}" == ""
  132.     Set dDir `getfilename -d -m 'Select folder with the patches.'`
  133.     Exit If "{dDir}" == ""
  134.  
  135.     For file In `files -r -s -f "{dDir}" -t TEXT`
  136.         If "{file}" =~ /{dDir}(≈)®1{DiffSuffix}/
  137.             Set src "{iDir}{®1}"
  138.             Set diff "{file}"
  139.             Set cvt "{src}{ConvertedSuffix}"
  140.  
  141.             Duplicate "{src}" "{cvt}" ∑∑ Dev:Null
  142.  
  143.             If "{Status}" != 0
  144.  
  145.                 Echo "Couldn’t find {cvt}. Skipping it."
  146.  
  147.             Else
  148.  
  149.                 Echo "Converting {src}."
  150.  
  151.                 Open "{cvt}"
  152.                 Target "{diff}"; find ∞
  153.  
  154.                 Loop
  155.  
  156. #
  157. # Look for the next change to make. Note that we check three times to see
  158. # if we've reached the top of the file (remember, we're searching backwards).
  159. # This is because a backwards search doesn't always flag the fact that it
  160. # failed; sometimes it does, and sometimes it doesn't. So I threw in two
  161. # more checks just to make sure. Without these checks, we can sometimes
  162. # enter into an infinite loop.
  163. #
  164.  
  165.                     Find \•≈∂(File\ || Break
  166.  
  167.                     (Evaluate "`Catenate §`" =~ /≈∂(File/) > Dev:Null || Break
  168.                     Set change "`Catenate §`"
  169.                     Break If "{change}" !~ /≈∂(File/
  170.  
  171.                     Find /Line [¬;]+;/
  172.                     (Evaluate "`Catenate §`" =~ /Line (≈)®1;/) > Dev:Null
  173.                     Set newSel "{®1}"
  174.  
  175.                     Find /Line ≈∂)/
  176.                     (Evaluate "`Catenate §`" =~ /Line (≈)®1∂)/) > Dev:Null
  177.                     Set oldSel "{®1}"
  178.  
  179.                     Find Δ!1
  180.                     If "{newSel}" =~ /([0-9]+)®8:([0-9]+)®9/
  181.                         Copy §:!`Evaluate "{®9}"-"{®8}"`
  182.                     Else
  183.                         Copy !0
  184.                     End
  185.  
  186.                     If "{change}" =~ /Extra lines in 2≈/
  187.                         Clear {oldSel} "{cvt}"
  188.                     Else
  189.                         Find {oldSel} "{cvt}"
  190.                         Paste § "{cvt}"
  191.                     End
  192.  
  193.                     Find \•≈∂(File≈∞\
  194.  
  195.                 End                        # Loop
  196.  
  197.                 Close -y "{cvt}"
  198.                 Close -n "{diff}"
  199.  
  200.                 Rename "{src}" "{src}{OriginalSuffix}"
  201.                 Rename "{cvt}" "{src}"
  202.             End                        # If {Status}
  203.         End ∑∑ "{WorkSheet}"    # If {file}
  204.     End                        # For file
  205. End
  206.  
  207. Set Exit 1